package com.randi.system.service.impl;

import com.github.pagehelper.PageInfo;
import com.randi.common.config.AppConfig;
import com.randi.common.constant.CacheConstants;
import com.randi.common.enums.ResultCode;
import com.randi.common.exception.ServiceException;
import com.randi.common.utils.page.PageHelperUtils;
import com.randi.system.domain.params.SysUserParam;
import com.randi.common.utils.SecurityUtils;
import com.randi.common.utils.StringUtils;
import com.randi.common.core.domain.entity.SysUser;
import com.randi.system.mapper.SysUserMapper;
import com.randi.system.service.ISysUserService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author ziye
 */
@Service
@CacheConfig(cacheNames = CacheConstants.SYS_USER)
public class SysUserServiceImpl implements ISysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private AppConfig.SystemConfig systemConfig;

    /**
     * 分页查询业务列表
     *
     * @param userParam 检索参数
     * @return 业务集合
     */
    @Override
    @Cacheable(keyGenerator = "baseKeyGenerator", unless = "#result == null")
    public PageInfo<SysUser> selectPageList(SysUserParam userParam) {
        PageHelperUtils.initPage(userParam);
        List<SysUser> sysUsers = sysUserMapper.selectSysUserList(userParam);
        return new PageInfo<>(sysUsers);
    }

    /**
     * 查询业务列表
     *
     * @param userParam 检索参数
     * @return 业务集合
     */
    @Override
    @Cacheable(keyGenerator = "baseKeyGenerator", unless = "#result == null or #result.isEmpty()")
    public List<SysUser> selectSysUserList(SysUserParam userParam) {
        return sysUserMapper.selectSysUserList(userParam);
    }

    /**
     * 查询用户信息
     *
     * @param userParam 检索参数
     * @return 业务集合
     */
    @Override
    public SysUser selectSysUser(SysUserParam userParam) {
        return sysUserMapper.selectSysUser(userParam);
    }

    /**
     * 根据ID查询用户信息
     *
     * @param userId userId
     * @return 用户信息
     */
    @Override
    @Cacheable(key = "#userId", unless = "#result == null")
    public SysUser selectSysUserById(Long userId) {
        SysUserParam userParam = new SysUserParam();
        userParam.setUserId(userId);
        return this.selectSysUser(userParam);
    }

    /**
     * 插入数据
     *
     * @param sysUser sysUser
     * @return 结果
     */
    @Override
    @CacheEvict(allEntries = true, beforeInvocation = true)
    public int insertSysUser(SysUser sysUser) {
        SysUser user = new SysUser();
        user.setUserName(sysUser.getUserName());
        user.setNickName(sysUser.getNickName());
        user.setPhoneNumber(sysUser.getPhoneNumber());
        user.setEmail(sysUser.getEmail());
        user.setStatus(sysUser.getStatus());
        user.setRemark(sysUser.getRemark());

        if (StringUtils.isNotEmpty(sysUser.getPassword())) {
            user.setPassword(new BCryptPasswordEncoder().encode(sysUser.getPassword()));
        } else {
            user.setPassword(systemConfig.getPassword().getValue());
        }
        SecurityUtils.setInsertInfo(user);
        return sysUserMapper.insert(user);
    }

    /**
     * 更新数据
     *
     * @param sysUser SysUser
     * @return 结果
     */
    @Override
    @CacheEvict(allEntries = true, beforeInvocation = true)
    public int updateSysUser(SysUser sysUser) {
        Long userAdminId = systemConfig.getUserAdminId();
        if (sysUser.getUserId().equals(userAdminId)) {
            throw new ServiceException(ResultCode.BUSINESS_EDITOR_ERROR_DISABLE);
        }
        SecurityUtils.setUpdateInfo(sysUser);
        return sysUserMapper.updateById(sysUser);
    }

    /**
     * 批量删除数据
     *
     * @param userIds userIds
     * @return status
     */
    @Override
    @CacheEvict(allEntries = true, beforeInvocation = true)
    public int deleteBatchSysUser(List<Long> userIds) {
        return sysUserMapper.batchUpdateDeleted(userIds);
    }

}
